package com.be.beadmin.device.controller;


import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.dto.output.InspectItemEx;
import com.be.beadmin.device.dto.output.InspectItemExcelEx;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;

import com.be.beadmin.device.service.InspectItemService;
import com.be.beadmin.device.dto.input.InspectItemQueryCriteria;
import com.be.beadmin.device.dto.input.InspectItemDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;

/**
* @author chenxu
* @date 2023-08-03
**/
@RestController
public class InspectItemController {

    @Autowired
    private  InspectItemService inspectItemService;


    @PostMapping(value = "/device/getInspectItemByPage")
    public ResultModel getInspectItemByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(inspectItemService.getInspectItemByPage(pageEntity)) ;
    }

    @PostMapping("/device/addInspectItem")
    public ResultModel addInspectItem(@RequestBody InspectItemDto inspectItemDto){
        inspectItemService.addInspectItem(inspectItemDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/device/delInspectItem")
    public ResultModel delInspectItem(@RequestBody InspectItemDto inspectItemDto){
        inspectItemService.delInspectItem(inspectItemDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateInspectItem")
    public ResultModel updateInspectItem(@RequestBody InspectItemDto inspectItemDto){
        inspectItemService.updateInspectItem(inspectItemDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/device/exportInspectItem")
    public void exportInspectItem(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<InspectItemEx> inspectItemPage = inspectItemService.getInspectItemByPage(pageEntity);
        List<InspectItemEx> inspectItemExes = inspectItemPage.getContent();
        List<InspectItemExcelEx> inspectItemExcelExes=inspectItemExes.stream().map(item->{
           InspectItemExcelEx excel=new InspectItemExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("InspectItem接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), InspectItemExcelEx.class).sheet("sheet1").doWrite(inspectItemExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), InspectItemExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(inspectItemExcelExes);
    }
}